package com.rzcoding.dao;

import com.rzcoding.dto.dao.CategoryLevelMo;
import com.rzcoding.dto.dao.CategoryLevelOneIdMo;
import com.rzcoding.entity.Category;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface CategoryMapper extends Mapper<Category> {
    @Select("SELECT c1.id,c1.level FROM category c1 LEFT JOIN category c2 ON c2.pid=c1.id LEFT JOIN category c3 ON c3.pid=c2.id GROUP BY c1.id HAVING c1.level = 0 order by c1.id")
    List<CategoryLevelOneIdMo> selectLevelOneIds();

    @Select("<script>"
            + "SELECT c1.id,c1.cat_name,c1.pid,c1.level, "
            + "c2.id AS c2_id,c2.cat_name AS c2_cat_name,c2.pid AS c2_pid,c2.level AS c2_level, "
            + "c3.id AS c3_id,c3.cat_name AS c3_cat_name,c3.pid AS c3_pid,c3.level AS c3_level "
            + "FROM category c1 "
            + "LEFT JOIN category c2 ON c2.pid=c1.id "
            + "LEFT JOIN category c3 ON c3.pid=c2.id "
            + "WHERE c1.id IN "
            + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "ORDER BY c1.id "
            + "</script>")
    List<CategoryLevelMo> selectLevelThreeByIds(@Param("ids") List<Integer> ids);

    @Select("SELECT c1.id,c1.cat_name,c1.pid,c1.level,c2.id AS c2_id,c2.cat_name AS c2_cat_name,c2.pid AS c2_pid,c2.level AS c2_level "
            + "FROM category c1 "
            + "LEFT JOIN category c2 ON c2.pid=c1.id AND c2.level = 1 "
            + "WHERE c1.level = 0 "
            + "ORDER BY c1.id ")
    List<CategoryLevelMo> selectLevelTwo();

}
